﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class ProfileSummary : ICPAdminPage
{
    public DataTable m_dtSource = new DataTable();

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Profile Summary";
        if (!IsPostBack && !IsCallback)
        {
        }

        InitGrid(false);
    }
    protected void cbkEditor_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = Handle_Editor(e.Parameter);
    }
    protected void dgMain_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        InitGrid(true);
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        Cached.ProfileSummary ccTmp = new Cached.ProfileSummary();
        m_dtSource = ccTmp.GetProfileSummaryList(bRefresh);

        dgMain.Caption = "ICP Company Profile Summary List";

        dgMain.DataSource = m_dtSource;
        dgMain.DataBind();
    }

    private String Handle_Editor(String sxParameter)
    {
        String sxReturn = String.Empty;
        String[] sxSplitted = sxParameter.Split(new String[] {KGlobal.DELIMITER}, StringSplitOptions.None);
        int iInputId = int.Parse(sxSplitted[1]);

        switch (sxSplitted[0].ToUpper())
        {
            case "ADD":
                Cached.ProfileSummary ccTmp = new Cached.ProfileSummary();
                DataView dv = new DataView(ccTmp.GetProfileSummaryList(), "INFO_TYPE_ID = " + iInputId, String.Empty, DataViewRowState.CurrentRows);

                if (dv.Count > 0)
                {
                    sxReturn = "That Info Type Summary has already exist!";
                }
                else
                {
                    DBQuery.ProfileSummaryTable.SaveProfileSummary(iInputId, sxSplitted[2]);
                }
                break;
            case "DEL":
                DBQuery.ProfileSummaryTable.DeleteProfileSummary(iInputId);
                break;
            case "UPDATE":
                DBQuery.ProfileSummaryTable.UpdateProfileSummary(iInputId, sxSplitted[2]);
                break;
            case "TOGGLE":
                DBQuery.ProfileSummaryTable.ToggleProfileSummary(iInputId);
                break;
            case "EDIT":
                sxReturn = Build_Editor(iInputId);
                break;
        }
        return sxReturn;
    }

    private String Build_Editor(int iProfileSummaryId)
    {
        String sxReturn = String.Empty;

        Cached.ProfileSummary ccTmp = new Cached.ProfileSummary();
        DataTable dtTmp = new DataView(
            ccTmp.GetProfileSummaryList(true), 
            "ID = " + iProfileSummaryId.ToString(), 
            String.Empty, DataViewRowState.CurrentRows).ToTable();

        if (dtTmp.Rows.Count > 0)
        {
            DataRow dr = dtTmp.Rows[0];
            sxReturn = AdminPageManager.BuildEditor_ProfileSummaryPopup(iProfileSummaryId, dr["TEXT"].ToString(), 
                dr["INFO_TYPE"].ToString() );
        }
        return sxReturn;
    }

    public String GetIcon(String sxActive)
    {
        return sxActive.ToUpper() == "Y" ? String.Empty : "0";
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods

    
}
